package com.yxs.go_bang.controller;

import com.yxs.go_bang.mapper.UserMapper;
import com.yxs.go_bang.model.User;
import com.yxs.go_bang.tools.Constant;
import com.yxs.go_bang.tools.ResponseBodyMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
public class UserController {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    @PostMapping("/login")
    public Object login(String username, String password, HttpServletRequest request){
        // 登录就是根据username去数据库中查找
        // 如果能找到匹配的用户，再去验证密码是否正确
        User user=userMapper.selectByName(username);
        System.out.println("[login]username:"+username);
        if (user==null|| !user.getPassword().equals(password)){
            // 登录失败
            System.out.println("登录失败");
            return new User(); // 返回空对象
        }
        HttpSession httpSession=request.getSession(true);
        httpSession.setAttribute("user",user);
        return user;
    }

    @PostMapping("/logout")
    public ResponseBodyMessage<Boolean> logout(HttpServletRequest request){
        HttpSession httpSession=request.getSession(false);
        if (httpSession==null){
            System.out.println("当前用户未登录，无法注销");
            return new ResponseBodyMessage<>(0,"当前用户未登录，无法注销",false);
        }
        httpSession.removeAttribute("user");
        System.out.println("注销成功");
        return new ResponseBodyMessage<>(1,"注销成功",true);
    }

    @PostMapping("/register")
    public Object register(String username,String password){
        try {
            User user=new User();
            user.setUsername(username);
            user.setPassword(password);
            userMapper.insert(user);
            return user;
        }catch (org.springframework.dao.DuplicateKeyException e){
            // 用户名重复
            User user=new User();
            return user;
        }
    }

    @GetMapping("/userinfo")
    public Object getUserInfo(HttpServletRequest request){
        try {
            HttpSession httpSession=request.getSession(false);
            User user=(User) httpSession.getAttribute("user");
            // 用户信息显示需要的是数据库中最新版本的user对象
            User newUser=userMapper.selectByName(user.getUsername());
            return newUser;
        }catch (NullPointerException nullPointerException){
            return new User();
        }

    }

    @PostMapping("/registermd")
    public ResponseBodyMessage<Boolean> resigterMd( String username,  String password){
        System.out.println("进入加密注册函数");
        User user=userMapper.selectByName(username);
        if (user!=null){
            return new ResponseBodyMessage<>(-1,"当前用户已存在",false);
        }
        String passwordMd=bCryptPasswordEncoder.encode(password);
        System.out.println("加密密码前："+password+"加密密码后："+passwordMd);
        User regUser=new User();
        regUser.setUsername(username);
        regUser.setPassword(passwordMd);
        userMapper.insert(regUser);
        return new ResponseBodyMessage<>(1,"注册完成",true);
    }

    @PostMapping("/loginmd")
    public ResponseBodyMessage<User> loginMd(@RequestParam String username, @RequestParam String password,HttpServletRequest request){
        System.out.println("加密登录函数");
        User user=userMapper.selectByName(username);
        if (user==null){
            return new ResponseBodyMessage<>(-1,"用户不存在",null);
        }else {
            boolean flg=bCryptPasswordEncoder.matches(password,user.getPassword());
            if (!flg){
                return new ResponseBodyMessage<>(-1,"密码错误",user);
            }
            request.getSession().setAttribute(Constant.USERINFO_SESSION_KEY,user);
            return new ResponseBodyMessage<>(1,"加密登录完成",user);
        }
    }
}
